#include <ipc.h>



void register_event_handler(unsigned char num, event_handler handler)
{
	ev_handlers[num] = handler;
}

void on_event(unsigned int signum, unsigned int sender)
{
	sig_queue[signum].signals[sig_queue[signum].next].sender = sender;
	sig_queue[signum].next++;
	if(sig_queue[signum].next == 256)
		sig_queue[signum].next = 0;
	sig_queue[signum].count++;
	
	if(ev_handlers[signum]!= 0)
		ev_handlers[signum](signum,sender);
		
	done();
}

void wait_for_event(unsigned int signum)
{
	unsigned int a = sig_queue[signum].count;
	while (sig_queue[signum].count == a)
	{
		;
	}
	return;
}


void wait_for_signal(unsigned int signum, unsigned int proc)
{
	wait_for_event(signum);
	while(sig_queue[signum].signals[sig_queue[signum].next-1].sender != proc)
	{
		wait_for_event(signum);
	}
	return;
}

void wait_ack(unsigned int process, unsigned int signum)
{
	unsigned int a = sig_queue[SIG_ACK].count;
	signal(process,signum);
	while(sig_queue[SIG_ACK].count == a)
	{
		;
	}
	while(sig_queue[SIG_ACK].signals[sig_queue[SIG_ACK].next-1].sender != process)
	{
		;
	}
	return;
}
